﻿using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Editor
{
    public static class EditorTool
    {
        [MenuItem("Tools/清理缓存 %#DEL")]
        static void ClearCache()
        {
            PlayerPrefs.DeleteAll();
            string paintDir = $"{Util.GetFolderInPersist("PaintResult")}/";
            Util.DeleteFolder(paintDir);
            Debug.Log("清理缓存完成");
        }

        static Component[] copiedComponents;

        [MenuItem("GameObject/Copy Current Components %#&C")]
        static void Copy()
        {
            copiedComponents = Selection.activeGameObject.GetComponents<Component>();
        }

        [MenuItem("GameObject/Paste Current Components %#&P")]
        static void Paste()
        {
            foreach (var targetGameObject in Selection.gameObjects)
            {
                if (!targetGameObject || copiedComponents == null) continue;
                foreach (var copiedComponent in copiedComponents)
                {
                    if (!copiedComponent) continue;
                    UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);
                    UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);
                }
            }
        }

        [MenuItem("GameObject/UI/Text")]
        public static void AddText()
        {
            Transform selection = Selection.activeTransform;
            if (selection != null)
            {
                GameObject obj = new GameObject();
                obj.name = "Text";
                obj.transform.SetParent(selection);
                var text = obj.AddComponent<Text>();
                text.horizontalOverflow = HorizontalWrapMode.Overflow;
                text.verticalOverflow = VerticalWrapMode.Overflow;
                text.color = Color.black;
                text.raycastTarget = false;
                text.alignment = TextAnchor.MiddleCenter;
                text.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/OtherAssets/Fonts/Arial Rounded Bold.ttf");
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
            }
        }

        [MenuItem("GameObject/UI/Button - Text")]
        public static void AddButtonText()
        {
            Transform selection = Selection.activeTransform;
            if (selection != null)
            {
                var prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/EditorExt/Button-Text.prefab");
                GameObject objBtn = Object.Instantiate(prefab, selection);
                objBtn.name = "Button";
            }
        }

        [MenuItem("GameObject/ResetTransform")]
        static void ResetTransform()
        {
            foreach (var transform in Selection.transforms)
            {
                transform.localPosition = Vector3.zero;
                transform.localRotation = Quaternion.Euler(0, 0, 0);
                transform.localScale = Vector3.one;
            }
        }
    }
}